package com.cloud.bc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcArticles;
import com.cloud.bc.model.entity.BcSetmeals;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.model.request.DistrictConvertRequest;
import com.cloud.bc.model.request.VoiceListRequest;
import com.cloud.bc.service.*;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@AllArgsConstructor
@Controller
@RequestMapping("m")
public class HomeController extends BaseController {

    private final BcSlideService bcSlideService;
    private final BcArticlesService bcArticlesService;
    private final BcSinglesService bcSinglesService;
    private final BcUsersService bcUsersService;
    private final BcSetmealsService bcSetmealsService;
    private final BcCategoryDistrictsService bcCategoryDistrictsService;

    /**
     * 推荐
     */
    @GetMapping("home")
    public ModelAndView center(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request,"推荐", "m/home");
        view.addObject("slide", bcSlideService.selectAll(null));
        //取文章推荐
        IPage<BcArticles> bcArticlesIPage = bcArticlesService.selectPage(new Page().setSize(3), null);
        view.addObject("article", bcArticlesIPage.getRecords());

        //官方推荐
        List<BcSingles> listOfficial = bcSinglesService.getOfficialRecommendation();
        //今日推荐
        Set<Integer> listOfficialUserId = listOfficial.stream().map(map -> map.getUserId()).collect(Collectors.toSet());
        String userIds = StrUtil.join(",", listOfficialUserId);
        List<BcSingles> listTodayRecommendation = bcSinglesService.getTodayRecommendation(userIds);
        view.addObject("listOfficial", listOfficial);
        view.addObject("todayData", bcSinglesService.respToDayRecommendationInfo(listTodayRecommendation));
        return view;
    }

    //声音
    @GetMapping("voice")
    public ModelAndView voice(HttpServletRequest request, HttpServletResponse response,
                              VoiceListRequest reqParam) {
        ModelAndView view = baseView(request,"声音", "m/voice");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        //        判断当前用户是否VIP，只有VIP才会显示相册，声音，视频
        boolean isVip = bcSinglesService.isVip(userInfo);

        DistrictConvertRequest districtConvertRequest = new DistrictConvertRequest();
        districtConvertRequest.setDistrict(reqParam.getDistrict());
        bcCategoryDistrictsService.getCityConvert(districtConvertRequest);
        BeanUtil.copyProperties(districtConvertRequest, reqParam);

        view.addObject("isVip", isVip);
        view.addObject("reqParam", reqParam);
        return view;
    }

    //视频
    @GetMapping("video")
    public ModelAndView video(HttpServletRequest request, HttpServletResponse response,
                              VoiceListRequest reqParam) {
        ModelAndView view = baseView(request,"视频", "m/video");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        //        判断当前用户是否VIP，只有VIP才会显示相册，声音，视频
        boolean isVip = bcSinglesService.isVip(userInfo);

        DistrictConvertRequest districtConvertRequest = new DistrictConvertRequest();
        districtConvertRequest.setDistrict(reqParam.getDistrict());
        bcCategoryDistrictsService.getCityConvert(districtConvertRequest);
        BeanUtil.copyProperties(districtConvertRequest, reqParam);

        view.addObject("isVip", isVip);
        view.addObject("reqParam", reqParam);
        return view;
    }

    //视频
    @GetMapping("video/show")
    public ModelAndView video(HttpServletRequest request, HttpServletResponse response,
                              String src) {
        ModelAndView view = baseView(request,"视频", "m/videoshow");
        view.addObject("src", src);
        return view;
    }

    //缘分
    @GetMapping("karma")
    public ModelAndView karma(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request,"缘分", "m/karma");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        boolean isVip = bcSinglesService.isVip(userInfo);
        view.addObject("isVip", isVip);
        return view;
    }

    //发现
    @GetMapping("find")
    public ModelAndView find(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request,"发现", "m/find");
        return view;
    }

    //附近
    @GetMapping("nearby")
    public ModelAndView nearby(HttpServletRequest request, HttpServletResponse response) {
        String gender = request.getParameter("gender");
        gender = StrUtil.isEmpty(gender) ? "" : gender;
        ModelAndView view = baseView(request,"附近", "m/nearby");
        view.addObject("gender", gender);
        return view;
    }

}
